<?php
/*
 * @Author: your name
 * @Date: 2021-11-04 16:42:10
 * @LastEditTime: 2021-11-24 20:47:18
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \property\app\Http\Controllers\admin_controller\Teaching.php
 */

namespace App\Http\Controllers\admin_controller;

use Illuminate\Support\Facades\Request;
use App\Models\admin_model\Role as RoleDb;
// use App\Models\admin_model\Admins;                        // admins表
use App\Models\admin_model\Column;                        // admins表
use App\Http\Controllers\redis\RedisController as Redis;  //redis 缓存

class Role extends BaseController
{
    /**
     * @description: 权限列表
     */
    public function roleList()
    {

        if (Request::has('keyWord')) {
            $whereStr = '';
            $limitStr = '';

            if (Request::input('pageSize') && Request::input('page')) {
                $limitStr =   limit_joint(Request::input('page'), Request::input('pageSize'));
            }
            $keyWord = array_filter(Request::input('keyWord'), 'filtrfunction');
            if (!empty($keyWord)) {
                $whereStr .= havaWhereLikeJoinEqual($keyWord);
            }


            $list  =   RoleDb::list($whereStr, $limitStr);

            $count =   RoleDb::count($whereStr);
            return $this->create(200, '查询成功', ['list' => $list, 'total' => $count[0]['count']]);
        } else if (Request::has('selectOrId')) {
            $fieldStr = '*';

            $whereStr = '';

            if (Request::has('field')) {
                $fieldStr = implode(',', Request::input('field'));
            }
            $selectOrId = Request::input('selectOrId');
            if ($selectOrId) {
                $whereStr  = ' OR `id` = ' . $selectOrId;
            }


            $list  =   RoleDb::selectList($fieldStr,  $whereStr);
            return $this->create(200, '查询成功',  $list);
        } else {
            return missingParameter();
        }
    }


    //添加
    public function roleAdd()
    {
        if (Request::has('formData')) {
            $formData = Request::input('formData');
            $res =  redis_mysql_add_return('role', $formData);
            if ($res) {
                return $this->create(200, '添加成功', ['id' => $res]);
            } else {
                return $this->create(100, '添加失败');
            }
        }
        // $teachingClass =  TeachingCulture::teachingClass();
        // return $this->create(200, '查询成功', $teachingClass);
    }



    //编辑
    public function roleEdit()
    {
        if (Request::has('formData') && Request::has('id')) {
            $formData = Request::input('formData');
            $id       = Request::input('id');
            if (is_array($id)) {
                $res = redis_mysql_edit_in('role', $id, $formData);
            } else {
                $res =  redis_mysql_edit_return('role', ['id' => $id], $formData);
                if ($res) {
                    $redisKey = 'role_' . $id;  //键值
                    $redisHname = 'PERMISSIONS';          //哈希键值名称
                    $permissions = \App\Http\Controllers\admin_controller\CheckRole::format(array('column_id' => $formData['column_id'], 'permission_id' => $formData['permission_id']));
                    Redis::hMset($redisKey, [$redisHname => serialize($permissions)]);  //存起来
                }
            }
            if ($res) {
                return $this->create(200, '修改成功');
            } else {
                return $this->create(100, '修改失败');
            }
        }
    }

    //查单条
    public function roleSelOne()
    {
        if (Request::has('id')) {
            $res =   redis_mysql_selone_return('role', Request::input('id'), false);
            return $this->create(200, '查询成功', $res);
        }
    }


    public function permission()
    {
        $columnPermission =  $this->getTree(Column::columnPermission());
        //   var_dump($columnPermission);

        return $this->create(200, '查询成功', $columnPermission);
    }


    private function getTree($column)
    {
        $column_tmp = [];
        $column_last = [];
        $checkedKeys = [];
        foreach ($column as $val) {
            if (!empty($val['operation']) && !empty($val['operation_id'])) {
                $operation =  explode(',', $val['operation']);
                $operation_id =  explode(',', $val['operation_id']);
                $tmp = [];
                for ($i = 0; $i < count($operation_id); $i++) {
                    $tmp[] =  array('value' => $operation_id[$i], 'label' => $operation[$i]);
                }
                $val['permission'] = $tmp;
            }

            $checkedKeys[] = $val['id'];

            $column_tmp[$val['id']] = $val;
        }

        foreach ($column_tmp as  &$val) {
            if ($val['pid'] == 0) {
                $column_last[] = &$val;
            } else {
                $column_tmp[$val['pid']]['children'][] =  &$val;
                $sort = array_column($column_tmp[$val['pid']]['children'], 'sort');              //提取数组某个字段
                array_multisort($sort, SORT_NUMERIC, $column_tmp[$val['pid']]['children']);     //按某个字段降序
            }
        }
        $sort = array_column($column_last, 'sort');              //提取数组某个字段
        array_multisort($sort, SORT_NUMERIC, $column_last);      //按某个字段降序
        //   print_r($column_last);
        return  array('column_last' => $column_last, 'checkedKeys' => $checkedKeys);
    }
}
